// 商品管理
<template>
  <div>
    <div class="user-hander">
      <h4>商品管理</h4>
      <div>
        <el-button type="success" size="mini" @click="handleAdd()" icon="el-icon-circle-plus-outline">
          新建商品</el-button>
        <el-button size="mini" icon="el-icon-refresh-left" @click="userRefresh">刷新</el-button>
      </div>
    </div>
    <!-- 显示数据 -->
    <el-table :data="
      tableData.filter(
        (data) =>
          !search || data.name.toLowerCase().includes(search.toLowerCase())
      )
    " border style="width: 100%">
      <el-table-column label="ID" prop="id" width="80"> </el-table-column>

      <el-table-column label="商品名称" prop="name"> </el-table-column>

      <el-table-column label="商品价格" prop="price"> </el-table-column>

      <el-table-column label="上架时间" prop="createTime"> </el-table-column>

      <el-table-column label="商品图片" prop="imgUrl">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.imgUrl" :fit="fit"></el-image>
        </template>
      </el-table-column>

      <el-table-column label="商品分类名称" prop="category"> </el-table-column>

      <el-table-column align="right">
        <template slot="header">
          <span>操作</span>
        </template>
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-remove-outline"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination @current-change="changePage" :page-size="5" :page-count="11" layout="prev, pager, next"
      :total="total">
    </el-pagination>
    <!-- 新建商品对话框 -->
    <el-dialog title="新建商品" :visible.sync="addFromVisible" width="30%" :before-close="handleClose">
      <el-form ref="addForm" :model="addForm" label-width="90px">
        <el-form-item label="商品名称">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>

        <el-form-item label="商品价格">
          <el-input v-model="addForm.price"></el-input>
        </el-form-item>

        <el-form-item label="上架时间">
          <el-col :span="11">
            <el-date-picker type="datetime" placeholder="选择日期" v-model="addForm.createTime"
              value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%"></el-date-picker>
          </el-col>
        </el-form-item>

        <el-form-item label="商品图片">
          <el-input v-model="addForm.imgUrl"></el-input>
        </el-form-item>

        <template>
          <el-form-item label="商品分类名称">
            <el-select v-model="addForm.categoryName" placeholder="请选择">
              <el-option v-for="item in categoryList" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </template>

        <el-form-item>
          <el-button type="primary" @click="onSubmit(addForm)">新建</el-button>
          <el-button @click="addFromVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 编辑商品对话框 -->
    <el-dialog title="编辑商品" :visible.sync="editFormVisible" width="30%" :before-close="handleClose">
      <el-form ref="editForm" :model="editForm" label-width="90px">
        <el-form-item label="商品ID" class="spanHiden">
          <p>{{ editForm.id }}</p>
        </el-form-item>

        <el-form-item label="商品名称">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>

        <el-form-item label="商品价格">
          <el-input v-model="editForm.price"></el-input>
        </el-form-item>

        <el-form-item label="上架时间">
          <el-col :span="11">
            <el-date-picker type="datetime" placeholder="选择日期" v-model="editForm.createTime"
              value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%"></el-date-picker>
          </el-col>
        </el-form-item>

        <el-form-item label="商品图片">
          <el-input v-model="editForm.imgUrl"></el-input>
        </el-form-item>

        <template>
          <el-form-item label="分类名称">
            <el-select v-model="editForm.categoryName" placeholder="请选择">
              <el-option v-for="item in categoryList" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </template>
        <el-form-item>
          <el-button type="primary" @click="OnEdit(editForm)">确定</el-button>
          <el-button @click="editFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 删除商品对话框 -->
    <el-dialog title="提示" :visible.sync="deleteVisible" width="30%" center>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Axios from "axios";
export default {
  data() {
    return {
      // 分类数据
      tableData: [],
      categoryList: [],
      value: "",
      //表头查询关键字
      search: "",
      //页码
      page: 1,
      //数据总数
      total: 100,
      //新建商品表单数据
      addForm: {
        name: "",
        price: "",
        createTime: "",
        imgUrl: "",
        categoryId: "",
      },
      // 编辑商品表单数据
      editForm: {
        name: "",
        price: "",
        createTime: "",
        imgUrl: "",
        categoryId: "",
      },
      //弹出框 Boolean
      addFromVisible: false,
      editFormVisible: false,
      deleteVisible: false,
    };
  },
  created() {
    this.getGoodsList();
    this.getCategoryList();
  },
  methods: {
    //新增商品数据行操作弹出框
    handleAdd() {
      this.addFromVisible = true;
    },
    //新增商品表单数据
    onSubmit(addForm) {
      console.log(addForm);
      Axios({
        url: "http://localhost:8080/goods/",
        method: "post",
        data: {
          name: addForm.name,
          price: addForm.price,
          createTime: addForm.createTime,
          imgUrl: addForm.imgUrl,
          categoryId: addForm.categoryName,
        },
      }).then((res) => {
        console.log(res);
        this.addForm = [];
        this.addFromVisible = false;
        this.getGoodsList();
      });
    },
    // 编辑商品表单数据
    OnEdit(editForm) {
      console.log(editForm);
      Axios({
        url: "http://localhost:8080/goods/",
        method: "put",
        data: {
          id: editForm.id,
          name: editForm.name,
          price: editForm.price,
          createTime: editForm.createTime,
          imgUrl: editForm.imgUrl,
          categoryId: editForm.categoryName,
        },
      }).then((res) => {
        console.log(res);
        this.editForm = [];
        this.editFormVisible = false;
        this.getGoodsList();
      });
    },
    //  编辑行
    handleEdit(index, row) {
      console.log("下标：" + index, "数据：" + row);
      // 显示编辑界面
      this.editFormVisible = true;
      this.getGoodsById(row.id);
    },
    //删除行
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("是否删除该商品数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delGoodsById(row);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消成功",
          });
        });
    },

    //加载页面
    openFullScreen() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
      });
      setTimeout(() => {
        loading.close();
      }, 500);
    },
    //刷新页面
    userRefresh() {
      this.search = "";
      this.getGoodsList();
    },
    // 根据id请求指定商品信息
    getGoodsById(id) {
      Axios.get("http://localhost:8080/goods/" + id).then((res) => {
        console.log("你点击的id" + id, res);
        this.editForm = res.data;
      });
    },
    // 获取分类信息
    getCategoryList() {
      Axios.get("http://localhost:8080/category/" + this.page + "/100").then(
        (res) => {
          console.log(res.data.data.list);
          this.categoryList = res.data.data.list;
        }
      );
    },

    // 请求商品信息
    getGoodsList() {
      this.openFullScreen();
      Axios.get("http://localhost:8080/goods/" + this.page + "/5").then(
        (res) => {
          console.log(res.data.list);
          this.tableData = res.data.list;
          this.total = res.data.total;
          console.log(this.total);
        }
      );
    },
    //删除商品请求
    delGoodsById(row) {
      Axios.delete("http://localhost:8080/goods/" + row.id)
        .then((res) => {
          // console.log(res);
          if (res.data.code == 0) {
            this.$message.error({
              message: res.data.msg,
            });
            return;
          }
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getGoodsList();
        })
        .catch((err) => {
          this.$message.error({
            message: "删除失败",
          });
          console.log(err);
        });
    },
    // 分页点击时间
    changePage(e) {
      console.log("切换到页数" + e);
      this.page = e;
      this.getGoodsList();
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => { });
    },
  },
};
</script>

<style>
.spanHiden {
  display: none;
  overflow: hidden;
}

.user-hander {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.el-pagination {
  text-align: left;
  background-color: white;
}
</style>